package com.ysxsoft.wdg.util.sp;

import android.content.Context;
import android.content.SharedPreferences;

import com.ysxsoft.wdg.MyApplication;


/**
 */
abstract class SharePrefBase {
    private final static String SP_NAME = "config";
    private static SharedPreferences sp;
    private static SharedPreferences getInstance(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp;
    }
    /**
     * 保存布尔值
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        getInstance(context).edit().putBoolean(key, value).apply();
    }

    /**
     * 保存字符串
     */
    public static void saveString(Context context, String key, String value) {
        getInstance(context).edit().putString(key, value).apply();
    }

    public static void deleteKey(Context context, String key) {
        getInstance(context).edit().putString(key, "").apply();
    }

    public static void clear() {
        getInstance(MyApplication.getContext()).edit().clear().apply();
    }

    /**
     * 保存long型
     */
    public static void saveLong(Context context, String key, long value) {
        getInstance(context).edit().putLong(key, value).apply();
    }

    /**
     * 保存int型
     */
    public static void saveInt(Context context, String key, int value) {
        getInstance(context).edit().putInt(key, value).apply();
    }

    /**
     * 保存float型
     */
    public static void saveFloat(Context context, String key, float value) {
        getInstance(context).edit().putFloat(key, value).apply();
    }

    /**
     * 获取字符值
     */
    public static String getString(Context context, String key, String defValue) {
        return getInstance(context).getString(key, defValue);
    }

    /**
     * 获取int值
     */
    public static int getInt(Context context, String key, int defValue) {
        return getInstance(context).getInt(key, defValue);
    }

    /**
     * 获取long值
     */
    public static long getLong(Context context, String key, long defValue) {
        return getInstance(context).getLong(key, defValue);
    }

    /**
     * 获取float值
     */
    public static float getFloat(Context context, String key, float defValue) {
        return getInstance(context).getFloat(key, defValue);
    }

    /**
     * 获取布尔值
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        return getInstance(context).getBoolean(key, defValue);
    }
}
